/**
 * Created by liubin on 2014/11/25.
 */
define(['auth/module'], function (module) {
    "use strict";
    module.registerController('LoginCtrl', function($scope, $state, $log, Login, Principal, NotificationService){
        $log.log('LoginCtrl');
        $scope.login = function(){
            $log.log('start login');
            // 这里根据ID来判断
            if($('#login-form').valid()){
                Login.login(
                    // args参数
                    {},
                    // POST参数
                    {
                        name: $scope.loginUser.name,
                        pwd: $scope.loginUser.pwd,
                        remember: $scope.loginUser.remember
                    },
                    // 更新成功的方法
                    function (success) {
                        if (success.success) {
                            // 登录成功
                            $log.log('login success');
                            Principal.identity(true).then(function (){
                                var isAuthenticated = Principal.isAuthenticated();
                                $log.log(isAuthenticated);
                                if(isAuthenticated){
                                    $state.go('app.dashboard');
                                }
                            });
                        }
                        //else{
                        //    // 登录失败
                        //    $log.log('login failed');
                        //    NotificationService.warning("错误", "登录失败", null, 6000);
                        //    //$.bigBox({
                        //    //    title: "错误",
                        //    //    content: "登录失败",
                        //    //    color: "#C46A69",
                        //    //    //timeout: 6000,
                        //    //    icon: "fa fa-warning shake animated",
                        //    //    //number: "1",
                        //    //    timeout: 6000
                        //    //});
                        //}
                    }
                );
            }
        }
    });
});